﻿using System;
using System.Collections.Generic;
using NmbpProject2.DataLayer;
using NmbpProject2.DboClasses;

namespace NmbpProject2.BusinessLayer
{
    internal class CategoryBL
    {
        public Category findByName(string name) {
            CategoryDL cDL = new CategoryDL();
            Category category = cDL.findByName(name);
            return category;
        }

        public void insertCategory(string name)
        {
            CategoryDL cDL = new CategoryDL();
            Category existingCategory = cDL.findByName(name);
            try
            {
                if (existingCategory != null)
                {
                    throw new Exception("There already exists a category with that name!");
                }
                else
                {
                    cDL.insertCategory(name);
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        public List<Category> getAllCategories()
        {
            CategoryDL cDL = new CategoryDL();
            List<Category> categories = cDL.getAllCategories();
            return categories;
        }

        public void delete(Category category, bool checkCascade)
        {
            CategoryDL catDL = new CategoryDL();
            try
            {
                CompApplicationDL caDL = new CompApplicationDL();
                CompetitionDL coDL = new CompetitionDL();
                List<CompApplication> applications = caDL.findByCategory(category, false);
                List<Competition> competitions = coDL.findByCategory(category, false);
                if (applications == null && competitions == null)
                {
                    catDL.delete(category);
                }
                else
                {
                    if (checkCascade)
                    {
                        string message = "There are ";
                        if (applications != null)
                        {
                            message = String.Concat(message, "competition applications");
                        }
                        if (applications != null && competitions != null)
                        {
                            message = String.Concat(message, " and ");
                        }
                        if (competitions != null)
                        {
                            message = String.Concat(message, "competitions");
                        }
                        message = String.Concat(
                            message, " in the database using the selected category(s)! Some might be deleted!"
                            );
                        throw new Exception(message);
                    }
                    else
                    {
                        // delete apps with single category
                        List<CompApplication> appsToDel = caDL.findByCategory(category, true);
                        if (appsToDel != null)
                        {
                            foreach (CompApplication app in appsToDel)
                            {
                                caDL.delete(app);
                                applications.Remove(app);
                            }
                        }
                        // delete comps with single category
                        List<Competition> compsToDel = coDL.findByCategory(category, true);
                        if (compsToDel != null)
                        {
                            foreach (Competition comp in compsToDel)
                            {
                                coDL.deleteCompetition(comp);
                                competitions.Remove(comp);
                            }
                        }
                        // remove category from apps with more than one category
                        if (applications != null)
                        {
                            foreach (CompApplication app in applications)
                            {
                                app.Categories.Remove(category);
                                caDL.update(app);
                            }
                        }
                        // remove category from comps with more than one category
                        if (competitions != null)
                        {
                            foreach (Competition comp in competitions)
                            {
                                comp.Categories.Remove(category);
                                coDL.updateCompetition(comp.Name, comp.Date, comp.Categories, comp.ProgLanguages, comp);
                            }
                        }
                        catDL.delete(category);
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void delete(List<Category> categories, bool checkCascade)
        {
            CategoryDL cDL = new CategoryDL();
            foreach (Category category in categories)
            {
                delete(category, checkCascade);
            }
        }

        public void update(Category category, string Name)
        {
            CategoryDL cDL = new CategoryDL();
            Category existingCategory = cDL.findByName(Name);
            try
            {
                if (existingCategory != null)
                {
                    throw new Exception("There already exists a category with that name!");
                }
                else
                {
                    cDL.update(category, Name);
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
    }
}